#!/usr/bin/env bash
# bash/profile
# ---
# github.com/rafi/.config

# Avoid PATH duplication inside Tmux sessions.
if [ -n "$TMUX" ] && [ -f /etc/profile ]; then
	# shellcheck disable=SC2123
	PATH=""
	source /etc/profile
fi

# Load all environment exports
# shellcheck disable=1091
source "$HOME/.config/bash/exports"

if [[ "$OSTYPE" != "darwin"* ]] && \
	[ -f "$XDG_CONFIG_HOME/systemd/user/console.target" ]; then
	# systemctl --user set-environment XDG_VTNR=1
	# Import environment on local login
	# systemctl --user import-environment
	# Bring up systemd --user to the specified target
	systemctl --user start console.target
fi

# Load interactive bash shell settings and utilities
if [ "$BASH" ]; then
	source "$XDG_CONFIG_HOME/bash/bashrc"
fi

# Use keychain to persist keys
# https://github.com/funtoo/keychain
if hash keychain 2>/dev/null; then
	eval "$(keychain --dir "$XDG_CACHE_HOME/keychain" --eval --agents ssh -Q --quiet current)"
fi

# Tmux: on login, attach to existing session or create a new one.
if [[ $- == *i* ]] && hash tmux 2>/dev/null && [ -z "$TMUX" ]; then
	# In SSH sessions, list tmux sessions on log-in.
	if [ -n "$SSH_TTY" ]; then
		tmux list-sessions 2>/dev/null | sed 's/^/# tmux /'
	else
		# Auto-attach (or new session)
		tmux attach 2>/dev/null || tmux new-session -A -s main
	fi
fi

# vim: set ft=sh ts=2 sw=0 tw=80 noet :
